package com.sys.orders.head.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.AAddress;
import com.sys.goods.pojo.AAddressEnum;
import com.sys.goods.service.IGoodsDbService;
import com.sys.orders.pojo.AOrdersComment;
import com.sys.orders.pojo.AOrdersDeliver;
import com.sys.orders.pojo.AOrdersDesc;
import com.sys.orders.pojo.AOrdersGoods;
import com.sys.orders.service.IOrdersDbService;
import com.sys.users.pojo.ACart;
import com.sys.users.pojo.AUsers;
import com.sys.users.pojo.AUsersEnum;
import com.sys.users.service.IUsersDbService;

/**
 * 前端的orderss
 * 
 * @author MyJjh
 *
 */
@Controller
@RequestMapping("/head/orders")
public class OrdersHeadController extends BaseController
{
	@Autowired
	private IOrdersDbService ordersDbService;
	@Autowired
	private IUsersDbService usersDbService;
	@Autowired
	private IGoodsDbService goodsDbService;

	/**
	 * 订单列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDescList")
	public String ordersDescList(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====ordersDescList======");
		/* 准备路径 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 生成分页对象 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 生成查询条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("userId", users.getUsersId());
		ApiResponse<AOrdersDesc> ordersDescList = this.ordersDbService.findCondListOrdersDescService(pageInfoUtil,
				condMap);

		request.setAttribute("response", ordersDescList.toJSON());
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		return "/head/order/ordersDescList";
	}

	/**
	 * 订单插入
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDescInsert")
	public String ordersDescInsert(HttpServletRequest request, HttpServletResponse response,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDescInsert========");
		/* 准备路径 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("userId", users.getUsersId());
		condMap.put("status", AUsersEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ACart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		/* 查询所有的收货地址 */
		condMap.clear();
		condMap.put("userId", users.getUsersId());
		condMap.put("status", AAddressEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AAddress> addressResponse = this.goodsDbService.findCondListAAddressService(null, condMap);
		addressResponse.clearInfo();
		/* 将结果存储到Request中 */
		model.addAttribute("addressResponse", addressResponse.toJSON());
		return "/head/order/ordersDescInsert";
	}

	/**
	 * 订单插入提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDescInsertSubmit")
	public String ordersDescInsertSubmit(HttpServletRequest request, HttpServletResponse response,Model model,
			AOrdersDesc ordersDesc)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDescInsertSubmit========");
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/*========查询购物车=========*/
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("userId", users.getUsersId());
		condMap.put("status", AUsersEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ACart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		ordersDesc.setUserId(users.getUsersId());
		
		/* 接收必要信息 */
		ApiResponse<Object> apiResponseDb = this.ordersDbService.saveOneOrdersDescService(ordersDesc);
		if(apiResponseDb.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
		{
			return "redirect:/head/orders/ordersDescList" ; 
		}
		return this.ordersDescInsert(request, response,model);
	}

	/**
	 * 订单更新
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDescUpdate")
	public String ordersDescUpdate(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDescUpdate========");
		String id = request.getParameter("id");
		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("orderId", Integer.valueOf(id));
		ApiResponse<AOrdersDesc> apiResponse = this.ordersDbService.findOneOrdersDescService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 同时查询出订单商品信息 */
		AOrdersDesc orders = apiResponse.getDataOneJava();
		condMap.clear();
		condMap.put("orderId", orders.getOrderId());
		condMap.put("extend", "true");
		ApiResponse<AOrdersGoods> ordersGoodsApiResponse = this.ordersDbService.findCondListAOrdersGoodsService(null, condMap);
		request.setAttribute("ordersGoodsResponse", ordersGoodsApiResponse.toJSON());
		
		
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType)) {
			return "/head/order/ordersDescUpdate";
		}else if("pay".equalsIgnoreCase(operType)) {
			return "/head/order/ordersDescPayUpdate";
		}
		return "/head/order/ordersDescInfo";
	}

	/**
	 * 订单更新提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDescUpdateSubmit")
	public String ordersDescUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDescUpdateSubmit========");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		String id = request.getParameter("id");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("orderId", Integer.valueOf(id));
		ApiResponse<AOrdersDesc> comment = this.ordersDbService.findOneOrdersDescService(condMap);
		AOrdersDesc ordersDesc = comment.getDataOneJava();
		request.setAttribute("response", apiResponse.toJSON());
		boolean flag = false ; 
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType)) {
			String status = request.getParameter("status");
			ordersDesc.setOrderStatus(Byte.valueOf(status));
			flag = true ; 
		}else if("pay".equalsIgnoreCase(operType)) {
			String status = request.getParameter("status");
			ordersDesc.setOrderStatus(Byte.valueOf(status));
			flag = true ; 
		}
		/* 接收参数 */
		if(flag) {
			apiResponse = this.ordersDbService.updateOneOrdersDescService(ordersDesc);
			ConstatFinalUtil.SYS_LOGGER.info(apiResponse.toJSON().toJSONString());
		}
		request.setAttribute("response", apiResponse);
		return "redirect:/head/orders/ordersDescList";
	}

	/**
	 * 评论列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersCommentList")
	public String ordersCommentList(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====ordersCommentList======");
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 生成分页对象 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 生成查询条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("userId", users.getUsersId());

		ApiResponse<AOrdersComment> ordersCommentList = this.ordersDbService
				.findCondListAOrdersCommentService(pageInfoUtil, condMap);

		request.setAttribute("response", ordersCommentList.toJSON());
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		return "/head/order/ordersCommentList";
	}

	/**
	 * 评论插入
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersCommentInsert")
	public String ordersCommentInsert(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersCommentInsert========");
		String id = request.getParameter("id");
		if(id == null) {
			id = "" ; 
		}
		if(!"".equalsIgnoreCase(id)) {
			request.setAttribute("id", id);
		}
		return "/head/order/ordersCommentInsert";
	}

	/**
	 * 评论插入提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersCommentInsertSubmit")
	public String ordersCommentInsertSubmit(HttpServletRequest request, HttpServletResponse response,
			AOrdersComment ordersComment)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersCommentInsertSubmit========");
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		ordersComment.setUserId(users.getUsersId());
		ApiResponse<Object> apiResponse = this.ordersDbService.saveOneAOrdersCommentService(ordersComment);
		request.setAttribute("response", apiResponse.toJSON());
		request.setAttribute("id", ordersComment.getOrderID());
		return this.ordersCommentInsert(request, response);
	}

	/**
	 * 评论更新
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersCommentUpdate")
	public String ordersCommentUpdate(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersCommentUpdate========");

		String id = request.getParameter("id");

		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("id", Integer.valueOf(id));
		ApiResponse<AOrdersComment> comment = this.ordersDbService.findOneAOrdersCommentService(condMap);

		AOrdersComment ordersComment = comment.getDataOneJava();

		request.setAttribute("ordersComment", ordersComment);

		return "/head/order/ordersCommentUpdate";
	}

	/**
	 * 评论更新提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersCommentUpdateSubmit")
	public String ordersCommentUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersCommentUpdateSubmit========");

		String id = request.getParameter("id");

		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("id", Integer.valueOf(id));
		ApiResponse<AOrdersComment> comment = this.ordersDbService.findOneAOrdersCommentService(condMap);

		AOrdersComment ordersComment = comment.getDataOneJava();

		/* 接收参数 */
		String orderId = request.getParameter("orderId");
		String commentContent = request.getParameter("commentContent");
		String stars = request.getParameter("stars");
		String status = request.getParameter("status");

		ordersComment.setOrderID(Integer.valueOf(orderId));
		ordersComment.setCommentContent(commentContent);
		ordersComment.setStars(Integer.valueOf(stars));
		ordersComment.setStars(Byte.valueOf(status));

		ApiResponse<Object> apiResponse = this.ordersDbService.updateOneAOrdersCommentService(ordersComment);
		ConstatFinalUtil.SYS_LOGGER.info(apiResponse.toJSON().toJSONString());

		return apiResponse.toJSON().toJSONString();
	}

	/**
	 * 发货明细列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDeliverList")
	public String ordersDeliverList(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====ordersDeliverList======");
		/* 生成分页对象 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 生成查询条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String orderDetailsId = request.getParameter("orderDetailsId");
		if(orderDetailsId == null) {
			orderDetailsId = "" ; 
		}
		condMap.put("orderDetailsId", orderDetailsId);

		ApiResponse<AOrdersDeliver> ordersDeliverList = this.ordersDbService
				.findCondListOrdersDeliverService(pageInfoUtil, condMap);

		request.setAttribute("response", ordersDeliverList.toJSON());
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		return "/head/order/ordersDeliverList";
	}

	/**
	 * 发货明细插入
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDeliverInsert")
	public String ordersDeliverInsertt(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDeliverInsert========");
		return "/head/order/ordersDeliverInsert";
	}

	/**
	 * 发货明细插入提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDeliverInsertSubmit")
	public String ordersDeliverInsertSubmit(HttpServletRequest request, HttpServletResponse response,
			AOrdersDeliver ordersDeliver)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDeliverInsertSubmit========");

		ApiResponse<Object> apiResponse = this.ordersDbService.saveOneOrdersDeliverService(ordersDeliver);

		return apiResponse.toJSON().toJSONString();
	}

	/**
	 * 发货明细更新
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDeliverUpdate")
	public String ordersDeliverUpdate(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDeliverUpdate========");

		String id = request.getParameter("id");

		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("id", Integer.valueOf(id));
		ApiResponse<AOrdersDeliver> deliver = this.ordersDbService.findOneOrdersDeliverService(condMap);

		AOrdersDeliver ordersDeliver = deliver.getDataOneJava();

		request.setAttribute("ordersDeliver", ordersDeliver);

		return "/head/order/ordersDeliverUpdate";
	}

	/**
	 * 发货明细更新提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDeliverUpdateSubmit")
	public String ordersDeliverUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersDeliverUpdateSubmit========");

		String id = request.getParameter("id");

		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("id", Integer.valueOf(id));
		ApiResponse<AOrdersDeliver> comment = this.ordersDbService.findOneOrdersDeliverService(condMap);

		AOrdersDeliver ordersDeliver = comment.getDataOneJava();

		/* 接收参数 */
		String orderDetailsId = request.getParameter("orderDetailsId");
		String blindBoxId = request.getParameter("blindBoxId");
		String purchaseQuantity = request.getParameter("purchaseQuantity");
		String status = request.getParameter("status");

		ordersDeliver.setOrderDetailsId(Integer.valueOf(orderDetailsId));
		ordersDeliver.setBlindBoxId(Integer.valueOf(blindBoxId));
		ordersDeliver.setPurchaseQuantity(Integer.valueOf(purchaseQuantity));
		ordersDeliver.setStatus(Byte.valueOf(status));

		ApiResponse<Object> apiResponse = this.ordersDbService.updateOneOrdersDeliverService(ordersDeliver);
		ConstatFinalUtil.SYS_LOGGER.info(apiResponse.toJSON().toJSONString());

		return apiResponse.toJSON().toJSONString();
	}

	/**
	 * 订单明细列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsList")
	public String ordersGoodsList(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====ordersGoodsList======");
		/* 生成分页对象 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 生成查询条件 */
		Map<String, Object> condMap = this.proccedSearch(request);

		ApiResponse<AOrdersGoods> ordersGoodsList = this.ordersDbService.findCondListAOrdersGoodsService(pageInfoUtil,
				condMap);

		request.setAttribute("response", ordersGoodsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		return "/head/order/ordersGoodsList";
	}

	/**
	 * 订单明细插入
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsInsert")
	public String ordersGoodsInsertt(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersGoodsInsert========");
		return "/head/order/ordersGoodsInsert";
	}

	/**
	 * 订单明细插入提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsInsertSubmit")
	public String ordersGoodsInsertSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersGoodsInsertSubmit========");
		/* 接收参数 */
		String orderId = request.getParameter("orderId");
		String blindBoxId = request.getParameter("blindBoxId");
		String settlementPrice = request.getParameter("settlementPrice");
		String goodsQuantity = request.getParameter("goodsQuantity");
		String status = request.getParameter("status");

		/* 创建对象 */
		AOrdersGoods ordersGoods = new AOrdersGoods();

		ordersGoods.setOrderId(Integer.valueOf(orderId));
		ordersGoods.setBlindBoxId(Integer.valueOf(blindBoxId));
		ordersGoods.setSettlementPrice(Integer.valueOf(settlementPrice));
		ordersGoods.setGoodsQuantity(Integer.valueOf(goodsQuantity));
		ordersGoods.setStatus(Byte.valueOf(status));

		ApiResponse<Object> apiResponse = this.ordersDbService.saveOneOrdersGoodsService(ordersGoods);

		return apiResponse.toJSON().toJSONString();
	}

	/**
	 * 订单明细更新
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsUpdate")
	public String ordersGoodsUpdate(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersGoodsUpdate========");

		String id = request.getParameter("id");

		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("id", Integer.valueOf(id));
		ApiResponse<AOrdersGoods> goods = this.ordersDbService.findOneAOrdersGoodsService(condMap);

		AOrdersGoods ordersGoods = goods.getDataOneJava();

		request.setAttribute("ordersGoods", ordersGoods);

		return "/head/order/ordersGoodsUpdate";
	}

	/**
	 * 订单明细更新提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersGoodsUpdateSubmit")
	public String ordersGoodsUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersGoodsUpdateSubmit========");

		String id = request.getParameter("id");

		Map<String, Object> condMap = new HashMap<String, Object>();

		condMap.put("id", Integer.valueOf(id));
		ApiResponse<AOrdersGoods> goods = this.ordersDbService.findOneAOrdersGoodsService(condMap);

		AOrdersGoods ordersGoods = goods.getDataOneJava();

		/* 接收参数 */
		String orderId = request.getParameter("orderId");
		String blindBoxId = request.getParameter("blindBoxId");
		String settlementPrice = request.getParameter("settlementPrice");
		String goodsQuantity = request.getParameter("goodsQuantity");
		String status = request.getParameter("status");

		ordersGoods.setOrderId(Integer.valueOf(orderId));
		ordersGoods.setBlindBoxId(Integer.valueOf(blindBoxId));
		ordersGoods.setSettlementPrice(Integer.valueOf(settlementPrice));
		ordersGoods.setGoodsQuantity(Integer.valueOf(goodsQuantity));
		ordersGoods.setStatus(Byte.valueOf(status));

		ApiResponse<Object> apiResponse = this.ordersDbService.updateOneOrdersGoodsService(ordersGoods);
		ConstatFinalUtil.SYS_LOGGER.info(apiResponse.toJSON().toJSONString());

		return apiResponse.toJSON().toJSONString();
	}
}
